const state = {
  cartInfo: localStorage.cartInfo ? JSON.parse(localStorage.cartInfo) : []
}
const mutations = {
  ADD_GOODS_TO_CART: (state, goodsInfo) => {
    state.cartInfo.push({
      goodsId: goodsInfo.ID,
      Name: goodsInfo.NAME,
      price: goodsInfo.PRESENT_PRICE,
      image: goodsInfo.IMAGE1,
      count: 1
    })
    localStorage.cartInfo = JSON.stringify(state.cartInfo)
  },
  REMOVE_GOODS_FORM_CART: (state, goodsId) => {
    state.cartInfo.splice(state.cartInfo.findIndex(item => { return item === goodsId }), 1)
    localStorage.cartInfo = JSON.stringify(state.cartInfo)
  },
  CHANGE_GOODS_COUNT: (state, goodsId, count) => {
    state.cartInfo[goodsId].count = count
    localStorage.cartInfo = JSON.stringify(state.cartInfo)
  },
  CLEAR_CRAT: state => {
    state.cartInfo = []
    localStorage.removeItem('cartInfo')
  }
}
const actions = {
  addGoodsToCart({ commit }, goodsInfo) {
    commit('ADD_GOODS_TO_CART', goodsInfo)
  },
  removeGoodsFormCart({ commit }, goodsId) {
    commit('REMOVE_GOODS_FORM_CART', goodsId)
  },
  changeGoodsCount({ commit }, goodsId, count) {
    commit('CHANGE_GOODS_COUNT', goodsId, count)
  },
  clearCart({ commit }) {
    commit('CLEAR_CRAT')
  }
}
export default { state, mutations, actions }